package com.llb.ble;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleNotifyCallback;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.llb.ble.app.comm.Observer;

import java.util.List;

public class SetActivity extends AppCompatActivity implements View.OnClickListener , Observer {
    public static final String KEY_DATA = "key_data";
    String uuid_service = "49535343-fe7d-4ae5-8fa9-9fafd205e455";
    String uuid_characteristic_write = "49535343-8841-43f4-a8d4-ecbe34729bb3";
    String uuid_characteristic_notify= "49535343-1e4d-4bd9-ba61-23c647249616";
    BluetoothGattCharacteristic xcharacteristic;
    private BleDevice bleDevice;
    ChartWebView chartWebView;
    TextView tv_fromble;
    EditText et_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        chartWebView = findViewById(R.id.chart);
        initBle();  //初始化蓝牙，开启 通知接收 来自于 蓝牙模块的数据
    }

    /**
     * 初始化蓝牙，蓝牙连接对象来自于 MainActicity Intent传递而来
     */
    private void initBle() {
        /*
        蓝牙连接对象
         */
        bleDevice = getIntent().getParcelableExtra(KEY_DATA);
        if (bleDevice == null) {
            finish();
        }
        /*
        开始通知，通知的 uuid数据来自于芯片文档
         */
        BleManager.getInstance().notify(
                bleDevice,
                uuid_service,
                uuid_characteristic_notify,
                new BleNotifyCallback() {
                    @Override
                    public void onNotifySuccess() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                            }
                        });
                    }

                    @Override
                    public void onNotifyFailure(final BleException exception) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                            }
                        });
                    }
                    /*
                    蓝牙模块发送给APP数据  调用的方法 data即比特数组
                     */
                    @Override
                    public void onCharacteristicChanged(final byte[] data) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String str = new String(data);
                                tv_fromble.setText(str);
                            }
                        });
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){


        }
    }
    /*
    向Ble发送数据
     */
    public void sendToBle(String sendStr){
        String hex = sendStr;
        /*
        把字符串 转变为 HEX
         */
        if (TextUtils.isEmpty(hex)) {
            return;
        }
        BleManager.getInstance().write(
                bleDevice,
                uuid_service,
                uuid_characteristic_write,
                hex.getBytes(),
                new BleWriteCallback() {

                    @Override
                    public void onWriteSuccess(final int current, final int total,
                                               final byte[] justWrite) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String str = new String(justWrite);
                                Log.d("Lanya...write", str);

                            }
                        });

                    }

                    @Override
                    public void onWriteFailure(final BleException exception) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
//                                addText(txt, exception.toString());
                            }
                        });
                    }
                });
    }
    @Override
    public void disConnected(BleDevice device) {
        if (device != null && bleDevice != null && device.getKey().equals(bleDevice.getKey())) {
            finish();
        }
    }
}
